package com.lyb.sort;

import java.util.Arrays;

/**
 * 快速排序
 */
public class QuickSort {

    public static void main(String[] args) {
        int[] data = {1,4,6,8,9,3,2,5,7,7};

        quickSort(data,0,data.length - 1);
    }
    public static void quickSort(int[] data, int low, int high){
        int i,j,temp,t;
        if (low > high){
            return;
        }

        i = low;
        j = high;
        //temp 是基准位
        temp = data[low];
        System.out.println("基准位 = " + temp);
        while (i < j){
            //先看右边，依次往左递减
            while (temp <= data[j] && i < j){
                j--;
            }
            //再看左边，依次往右递增
            while (temp >= data[i] && i < j){
                i++;
            }
            //如果满足条件则交换
            if (i < j){
                t = data[j];
                data[j] = data[i];
                data[i] = t;
//                System.out.println(Arrays.toString(data));
            }
        }
        //最后将基准位与i和j相等位置的数字交换
        data[low] = data[i];
        data[i] = temp;
        System.out.println(Arrays.toString(data));
        //递归调用左半边数组
        quickSort(data,low,j - 1);
        //递归调用右半边数组
        quickSort(data,j + 1, high);

    }
}
